﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LinkCard.AdminCP.Common;

namespace LinkCard.AdminCP
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            error.Visible = false;
        }

        protected void checkLogin(string sUsername, string sUserPw, string loginAs)
        {

            string sSQL = "SELECT UserID FROM [User] WHERE Username='" + sUsername + "' AND Password ='" + sUserPw + "'";
            DataTable dt = DataHelper.SelectTable(sSQL, false);
            if (dt.Rows.Count > 0)
            {
                Session["USER"] = sUsername;
                Session["USERID"] = dt.Rows[0]["UserID"].ToString();
                Session["LOGINAS"] = loginAs; //0: quản trị hệ thống, 1: quản trị tin tức
                Session.Timeout = 19;
                Response.Redirect("dashboard.aspx");
            }
            else
            {
                error.Visible = true;
            }


        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string sUsername = username.Value.ToUpper().Trim(); 
            sUsername = hash1way.SQLPatch(sUsername);
            string sUserPw = adminpassword.Value.ToUpper().Trim(); 
            sUserPw = hash1way.SQLPatch(sUserPw);
            string loginAs = loginas.SelectedIndex.ToString();
            checkLogin(sUsername, sUserPw, loginAs);
        }
    }
}